//
//  SlideAnimator.swift
//  CustomTransition
//
//  Created by CaryaLiu on 2021/10/14.
//

import UIKit

/// push: slide up
class SlidePushAnimator: NSObject, UIViewControllerAnimatedTransitioning {
    var direction = TransitionType.Direction.up
    init(direction: TransitionType.Direction) {
        self.direction = direction
    }
    
    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
//        TimeInterval(UINavigationController.hideShowBarDuration)
        0.5
    }
    
    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
        guard let fromView = transitionContext.view(forKey: .from),
        let toView = transitionContext.view(forKey: .to) else { return }
        
        transitionContext.containerView.addSubview(toView)
        let size = transitionContext.containerView.bounds.size
        switch direction {
        case .up:
            toView.frame = CGRect(x: 0, y: size.height, width: size.width, height: size.height)
        case .down:
            toView.frame = CGRect(x: 0, y: -size.height, width: size.width, height: size.height)
        case .left:
            toView.frame = CGRect(x: size.width, y: 0, width: size.width, height: size.height)
        case .right:
            toView.frame = CGRect(x: -size.width, y: 0, width: size.width, height: size.height)
        }
        
        UIView.animate(withDuration: transitionDuration(using: transitionContext), delay: 0, options: .curveEaseInOut) {
            toView.frame = CGRect(x: 0, y: 0, width: size.width, height: size.height)
            switch self.direction {
            case .up:
                fromView.frame = CGRect(x: 0, y: -size.height, width: size.width, height: size.height)
            case .down:
                fromView.frame = CGRect(x: 0, y: size.height, width: size.width, height: size.height)
            case .left:
                fromView.frame = CGRect(x: -size.width, y: 0, width: size.width, height: size.height)
            case .right:
                fromView.frame = CGRect(x: size.width, y: 0, width: size.width, height: size.height)
            }
        } completion: { finished in
            transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
        }
    }
}

// pop: slide down
class SlidePopAnimator: NSObject, UIViewControllerAnimatedTransitioning {
    var direction = TransitionType.Direction.up
    init(direction: TransitionType.Direction) {
        self.direction = direction
    }
    
    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
        0.5
    }
    
    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
        guard let fromView = transitionContext.view(forKey: .from),
              let toView = transitionContext.view(forKey: .to) else { return }
        
        transitionContext.containerView.insertSubview(toView, belowSubview: fromView)
        let size = transitionContext.containerView.bounds.size
        switch direction {
        case .up:
            toView.frame = CGRect(x: 0, y: size.height, width: size.width, height: size.height)
        case .down:
            toView.frame = CGRect(x: 0, y: -size.height, width: size.width, height: size.height)
        case .left:
            toView.frame = CGRect(x: size.width, y: 0, width: size.width, height: size.height)
        case .right:
            toView.frame = CGRect(x: -size.width, y: 0, width: size.width, height: size.height)
        }
        
        UIView.animate(withDuration: transitionDuration(using: transitionContext), delay: 0, options: .curveEaseInOut) {
            switch self.direction {
            case .up:
                fromView.frame = CGRect(x: 0, y: -size.height, width: size.width, height: size.height)
            case .down:
                fromView.frame = CGRect(x: 0, y: size.height, width: size.width, height: size.height)
            case .left:
                fromView.frame = CGRect(x: -size.width, y: 0, width: size.width, height: size.height)
            case .right:
                fromView.frame = CGRect(x: size.width, y: 0, width: size.width, height: size.height)
            }
            toView.frame = CGRect(x: 0, y: 0, width: size.width, height: size.height)
        } completion: { finished in
            transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
        }
    }
}
